package dao;

import model.TDrug;
import utils.DataUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DrugDAOImpl implements DrugDAO{
    private DataUtil dataUtil = new DataUtil();

    /**
     * 添加一个药品信息
     *
     * @param record 存储药品信息的实体对象
     * @return 药品主键ID
     */
    @Override
    public int addDrug(TDrug record) {
        String sql = "insert into t_drug(drugid,name,img,drugclass,cureclass,drugsize,fexplain,merchant,price,userid)"
                +"values(?,?,?,?,?,?,?,?,?,?)";
        Object [] para = {record.getDrugid(),record.getName(),record.getImg(),record.getDrugclass(),record.getCureclass(),record.getDrugsize(),record.getFexplain(),record.getMerchant(),record.getPrice(),record.getUserid()};
        int a = dataUtil.insert(sql,para);
        return a;
    }


    /**
     * 修改药品信息
     *
     * @param record 存储修改的药品信息
     * @return 成功返回true，失败返回false
     */
    @Override
    public boolean updateDrug(TDrug record) {
        String sql = "update t_drug set name = ?,img = ?,drugclass = ?,cureclass = ?,drugsize = ?,fexplain = ?,merchant = ?,price = ? where drugid = ?";
        Object [] para = {record.getName(),record.getImg(),record.getDrugclass(),record.getCureclass(),record.getDrugsize(),record.getFexplain(),record.getMerchant(),record.getPrice(),record.getDrugid()};
        int a = dataUtil.update(sql,para);
        if(a>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 删除指定ID的药品品
     *
     * @param drugid
     * @return 成功返回true，失败返回false
     */
    @Override
    public boolean deleteDrug(int drugid) {
        String sql = "delete from t_drug where drugid = ?";
        Object [] para = {drugid};
        int a = dataUtil.delete(sql,para);
        if(a>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据商品ID查询商品
     *
     * @param drugid
     * @return
     */
    @Override
    public TDrug getDrugById(int drugid) {
        String sql = "select * from t_drug where drugid = ?";
        Object [] para = {drugid};
        ResultSet rs = dataUtil.select(sql,para);
        TDrug tDrug  = new TDrug(rs);
        return tDrug;
    }

    /**
     * 查询所有的商品
     *
     * @return List<TDrug>
     */
    @Override
    public List<TDrug> getAllDrug() throws SQLException {
        List<TDrug> ar = new ArrayList<>();
        String sql = "select * from t_drug";
        Object [] para = null;
        ResultSet rs = dataUtil.select(sql,para);
        ResultSet rss = dataUtil.select(sql,para);
        int a = dataUtil.lineNum(rss);
        while(a!=0){
            TDrug tProduct = new TDrug(rs);
            ar.add(tProduct);
            a--;
        }
        rs.close();
        rss.close();
        return ar;
    }


    /**
     * 查询用户下面所有的商品
     *
     * @return List<TDrug>
     */
    public List<TDrug> getUserAllDrug(String userid) throws SQLException {
        List<TDrug> ar = new ArrayList<>();
        String sql = "select * from t_drug where userid = ?";
        Object [] para = {userid};
        ResultSet rs = dataUtil.select(sql,para);
        ResultSet rss = dataUtil.select(sql,para);
        int a = dataUtil.lineNum(rss);
        while(a!=0){
            TDrug tProduct = new TDrug(rs);
            ar.add(tProduct);
            a--;
        }
        rs.close();
        rss.close();
        return ar;
    }

    /**
     * 根据商品名称模糊查询商品
     *
     * @param name 商品名称
     * @return List<TDrug>
     */
    @Override
    public List<TDrug> getTDrugsByTitle(String name) throws SQLException {
        List<TDrug> ar = new ArrayList<>();

        if(name!=null&&!name.equals("")) {
            String sql = "select * from t_drug where name like ?";
            Object [] para = {"%"+name+"%"};

            ResultSet rs = dataUtil.select(sql,para);
            ResultSet rss = dataUtil.select(sql,para);
            int a = dataUtil.lineNum(rss);
            while(a!=0){
                TDrug tProduct = new TDrug(rs);
                ar.add(tProduct);
                a--;
            }
            rs.close();
            rss.close();
            return ar;
        }else{
            String sql = "select * from t_drug";
            Object [] para = null;
            ResultSet rs = dataUtil.select(sql,para);
            ResultSet rss = dataUtil.select(sql,para);
            int a = dataUtil.lineNum(rss);
            while(a!=0){
                TDrug tProduct = new TDrug(rs);
                ar.add(tProduct);
                a--;
            }
            rs.close();
            rss.close();
            return ar;
        }
    }
}
